package com.dasberg.guice;

import org.apache.log4j.Appender;
import org.apache.log4j.Layout;
import org.apache.log4j.Level;
import org.apache.log4j.spi.ErrorHandler;
import org.apache.log4j.spi.Filter;
import org.apache.log4j.spi.LoggingEvent;

import java.util.ArrayList;
import java.util.List;

/**
 * Logger appender for test purposes.
 * @author mischa
 */
public class LoggingTestAppender implements Appender {
    private List<Object> messages;

    public LoggingTestAppender() {
        messages = new ArrayList<Object>();
    }

    @Override
    public void addFilter(Filter filter) {
    }

    @Override
    public Filter getFilter() {
        return null;
    }

    @Override
    public void clearFilters() {
    }

    @Override
    public void close() {
    }

    @Override
    public void doAppend(LoggingEvent loggingEvent) {
        if (loggingEvent.getLevel().equals(Level.ERROR)) {
            messages.add(loggingEvent.getMessage());
        }
    }

    @Override
    public String getName() {
        return null;
    }

    @Override
    public void setErrorHandler(ErrorHandler errorHandler) {
    }

    @Override
    public ErrorHandler getErrorHandler() {
        return null;
    }

    @Override
    public void setLayout(Layout layout) {
    }

    @Override
    public Layout getLayout() {
        return null;
    }

    @Override
    public void setName(String s) {
    }

    @Override
    public boolean requiresLayout() {
        return false;
    }

    public List<Object> getMessages() {
        return messages;
    }
}
